package cn.wolfcode.p2p.hosting.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.business.domain.RechargeOnline;
import cn.wolfcode.p2p.hosting.service.IHostingService;
import cn.wolfcode.p2p.hosting.util.HostingUtil;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class HostingServiceImpl implements IHostingService {

    @Override
    public void createAccount(LoginInfo loginInfo) {
        /**
         * 准备签名
         * 把签名设置给基本参数
         * 调用创建账户接口,发送请求
         * 接收结果,处理回签
         */
        Map<String, String> baseParamMap = HostingUtil.getBaseParamMap();
        baseParamMap.put("service", "create_activate_member");
        baseParamMap.put("identity_id", HostingUtil.ACCOUNT_PREFIX + loginInfo.getId());
        baseParamMap.put("identity_type", "UID");
        baseParamMap.put("member_type", "1");
        baseParamMap.put("client_ip", UserContext.getIp());

        //准备签名
        // 下面两句放在sendRequest中方便 createAccount 和 recharge 2个方法调用
        // String signString = HostingUtil.sign(baseParamMap);

        //把签名设置给基本参数
        // baseParamMap.put("sign", signString);

        //发送请求
        HostingUtil.sendRequest(baseParamMap,HostingUtil.URL_MGS);

    }

    @Override
    public String recharge(RechargeOnline re) {

        //准备基本参数
        Map<String, String> baseParamMap = HostingUtil.getBaseParamMap();

        //业务参数
        baseParamMap.put("service", "create_hosting_deposit");
        baseParamMap.put("out_trade_no", re.getTradeCode());
        baseParamMap.put("identity_id", HostingUtil.ACCOUNT_PREFIX + re.getApplier().getId());
        baseParamMap.put("identity_type", "UID");
        baseParamMap.put("account_type", "BASIC");
        String amount = re.getAmount().toString();
        baseParamMap.put("amount", amount);
        baseParamMap.put("payer_ip", UserContext.getIp());
        baseParamMap.put("pay_method", "SINAPAY^" + amount);

        //系统异步回调通知地址,通知充值结果
        baseParamMap.put("notify_url", "http://localhost:8282/rechargeNotify");

        //跳转充值结果的跳转页面路径
        baseParamMap.put("return_url", "http://localhost:8282/rechargeResult?tradeCode=" + re.getTradeCode());



        //发送请求
        return HostingUtil.sendRequest(baseParamMap,HostingUtil.URL_MAS,true);
    }
}
